package com.bort.system.utils;

import com.bort.system.BortApplication;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.util.Timer;
import java.util.TimerTask;

public class ToastUtils {

    private static Stage stage=new Stage();
    private static Label label=new Label();

    static {
        stage.initStyle(StageStyle.TRANSPARENT);//舞台透明
        stage.initOwner(BortApplication.getStage());
    }


    /**
     * 指定时间消失
     * @param msg
     * @param time
     */
    public static void toast(String msg, int time) {
        label.setText(msg);
        TimerTask task= new TimerTask() {
            @Override
            public void run() {
                Platform.runLater(()->stage.close());
            }
        };
        initToast(msg);
        Timer timer=new Timer();
        timer.schedule(task,time);
        stage.show();
    }

    //默认3秒
    public static void toast(String msg) {
        toast(msg,3000);
    }

    //设置消息
    private static void initToast(String msg) {
        Label label=new Label(msg);//默认信息
        label.setStyle("-fx-background: rgba(56,56,56,0.7);-fx-border-radius: 5;-fx-background-radius: 5");//label透明,圆角
        label.setTextFill(Color.rgb(225,226,226));//消息字体颜色
        label.setPadding(new Insets(5,10,5,10));
        label.setFont(new Font(16));//字体大小
        label.setPrefWidth(180);
        label.setTextAlignment(TextAlignment.CENTER);
        label.setAlignment(Pos.CENTER);
        Scene scene=new Scene(label);
        scene.setFill(null);//场景透明
        stage.setScene(scene);
        double parentX = stage.getOwner().getX();
        double parentY = stage.getOwner().getY();
        stage.setX(parentX + 210);
        stage.setY(parentY + 50);
    }

}
